<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/18
 * Time: 14:41
 */
namespace app\api\controller;
use think\Db;
use org\Qiniu;
class Timing extends Base {
    /**
     * 定时器发送消息
     */
    public function sendNotice(){
        set_time_limit(0);
        //定时器1：解答超过1小时未提交则清除状态;定时器3：未答题的一小时后关闭
        //1-1：自营订单的超时自动取消
        $today = date('Y-m-d H:i:s');
        $where['n_send'] = 1;
        $where['n_auto_time'] = ['<=',$today];
        $field = 'n_id,n_range,n_user_id,n_title';
        $data = Db::table('up_notice')
            ->where($where)
            ->field($field)
            ->select();
//        dump($res);die;
        if($data){
            foreach ($data as $k=>$info){
                $n_id = $info['n_id'];
                if($info['n_range'] == 1){
                    $ids = Db::table('up_u_user')
                        ->where('user_status',1)
                        ->where('user_id','<>',0)
                        ->column('user_id');
                }else{
                    $ids = explode(',', trim($info['n_user_id'], ','));
                }
                $res = Db::name('notice')->where('n_id', $n_id)->update([
                    'n_send' => 2
                ]);
                if($res){
                    $list = [];//需要推送的用户数组
                    foreach ($ids as $k => $v) {
                        $val = (string)$v;
                        array_push($list, $val);
                    }
                    try {
                        $this->Jpush(1, $list, $info['n_title'], ['nid' => $n_id, 'type' => 1]);
                    } catch (\Exception $e) {
                    }
                }
            }
        }

//        $time = time();
//        $one_hour_ago = date("Y-m-d H:i:s",($time - 1800));
        $res = Db::table('up_plan_info')
            ->where('plan_online',1)
            ->where('plan_status',1)
            ->where('plan_notice_pass',1)
            ->where('plan_start_time','<=',$today)
            ->field('plan_number,plan_typename,plan_teacher,plan_id')
            ->select();
        if($res){
            foreach ($res as $k => $v) {
                try{
                    $con = '您的课程已超过上课时间，请知悉！课程编号：' . $v['plan_number'] . '，名字：' . $v['plan_typename'];
                    $nid = allInNotice('您的课程已超过上课时间',$con,8,$v['plan_teacher'],$v['plan_id']);
                    $this->Jpush(1, $v['plan_teacher'], '您的课程已超过上课时间', ['type'=>8,'nid'=>$nid]);
                }catch(\Exception $e){

                }
                Db::table('up_plan_info')
                    ->where('plan_id',$v['plan_id'])
                    ->setField('plan_notice_pass',2);
            }
        }
    }


    public function dowload() {//echo 1;die;
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        if(strpos($agent, 'iphone') || strpos($agent, 'ipad'))
        {
            echo "<script> location.href='https://itunes.apple.com/cn/app/id1516439349'; </script>";
        }else{
           echo "<script> location.href='https://a.app.qq.com/o/simple.jsp?pkgname=com.paixun.fz'; </script>";
        }

    }

    /**
     * 获取引导页
     * @return \think\response\Json
     */
    public function getYinDao() {
        $lunbo = Db::table('up_appstart')
            ->where('banner_status',1)
            ->field('banner_img as url')
            ->order('banner_sort ASC')
            ->page (1,5)
            ->select();
        return json(['status'=>200,'msg'=>$lunbo]);
    }
}